﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace f1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
          
            SetListView();
            TreeNode rootNode = new TreeNode("Users", 0, 0);
            rootNode.Name = "Users";
            rootNode.Text = "Users";
           
            this.treeView1.Nodes.Add(rootNode);
            string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
          
      
            SetListView();
          



        }
        private void SetListView()
        { 
            //行和列是否显示网格线
            this.listView1.GridLines = false;
            //显示方式(注意View是Details详细显示)
            this.listView1.View = View.Details;
            //是否可编辑
            this.listView1.LabelEdit = true;
            //没有足够的空间显示时,是否添加滚动条
            this.listView1.Scrollable = true;
            //对表头进行设置
            this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
            //是否可以选择行
            this.listView1.FullRowSelect = true;

            //设置listView列标题头 宽度为9/13 2/13 2/13 
            //其中设置标题头自动适应宽度,-1根据内容设置宽度,-2根据标题设置宽度
            this.listView1.Columns.Add("名称", 9 * listView1.Width / 13);
            this.listView1.Columns.Add("大小", 2 * listView1.Width / 13);
            this.listView1.Columns.Add("类型", 2 * listView1.Width / 13);
        }
        private string GetPathFromNode(TreeNode node)
        {
           
            if (node.Name == "Users")
                return "C:\\Users";
            //注意:树形控件中我只赋值Tag\Name,使用Text时赋值即可使用
           
            //Path.Combine组合产生路径 如 Path.Combine("A","B")则生成"A\\B"
            return Path.Combine(GetPathFromNode(node.Parent), node.Name);
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode clickedNode = e.Node;
            listView1.Items.Clear();
         
            //获取指定目录中的子目录名称并加载结点
           string mypath = GetPathFromNode(clickedNode);
           
          if (Path.GetExtension(mypath) == "")
            {
                //获取当前目录文件列表
                string[] dics = Directory.GetDirectories(mypath);
                string[] best = Directory.GetFiles(mypath);
                foreach (string my in dics)
                {
                    TreeNode you = new TreeNode();
                    you.Name = Path.GetFileName(my);
                    you.Text = Path.GetFileName(my);
                    if (Path.GetExtension(my) == ".txt")
                        you.ImageIndex = 1;
                    else
                        you.ImageIndex = 0;

                    ListViewItem listItem = new ListViewItem();
                    //listItem.SubItems[0].Text = fileInfo[i].Name;             //文件名(方法二)
                    listItem.Text = Path.GetFileName(my);   //显示文件名
                                                            //设置行颜色
                    clickedNode.Nodes.Add(you);
                    listItem.ImageIndex = 0;
                    //length/1024转换为KB字节数整数值 Ceiling返回最小整数值 Divide除法
                    //获取当前文件大小


                    //获取文件最后访问时间
                    //listItem.SubItems.Add(fileInfo[i].LastWriteTime.ToString());

                    //获取文件扩展名时可用Substring除去点 否则显示".txt文件"

                    //加载数据至filesList
                    this.listView1.Items.Add(listItem);
                }
                foreach (string my in best)
                {
                    TreeNode you = new TreeNode(new DirectoryInfo(my).Name);
                    you.Name = Path.GetFileName(my);
                    you.Text = Path.GetFileName(my);
                    you.ImageIndex = 1;
                    ListViewItem listItem = new ListViewItem();
                    //listItem.SubItems[0].Text = fileInfo[i].Name;             //文件名(方法二)
                    listItem.Text = Path.GetFileName(my);   //显示文件名
                                                            //设置行颜色
                    clickedNode.Nodes.Add(you);
                    listItem.ImageIndex = 1;
                    //length/1024转换为KB字节数整数值 Ceiling返回最小整数值 Divide除法
                    //获取当前文件大小


                    //获取文件最后访问时间
                    //listItem.SubItems.Add(fileInfo[i].LastWriteTime.ToString());

                    //获取文件扩展名时可用Substring除去点 否则显示".txt文件"

                    //加载数据至filesList
                    this.listView1.Items.Add(listItem);
                }
            }
            if (Path.GetExtension(mypath) == ".txt")
            {
                System.Diagnostics.Process.Start(mypath);
            }



        }
  
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
   
      
}
